package cn.gson.springboot.model.service;

import cn.gson.springboot.model.dao.FunctionDao;
import cn.gson.springboot.model.dao.JobDao;
import cn.gson.springboot.model.mapper.FunctionMapper;
import cn.gson.springboot.model.mapper.FunctionTypeMapper;
import cn.gson.springboot.model.pojos.Function;
import cn.gson.springboot.model.pojos.FunctionType;
import cn.gson.springboot.model.pojos.Job;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FunctionService {
    @Autowired
    FunctionMapper fm;
    @Autowired
    JobDao jd;
    @Autowired
    FunctionDao fd;

    //查找职位可以授权的权限
    public List<Function> allFuns(Integer jobId){
        return fm.allFuns(jobId);
    }

    //查找职位已有的权限
    public List<Integer> jobFuns(Integer jobId){
        return fm.jobFuns(jobId);
    }


    public List<Function> funVague(String content){
        return  fm.funVague(content);
    }

    public void saveJobJurisdiction(Integer jobId,List<Integer> funsId){
        Job job = jd.findById(jobId).get();
        List<Function> funs = job.getFuns();
        if (funs == null){
            funs = new ArrayList<>();
        }
        funs.clear();
        funs.addAll((Collection) fd.findAllById(funsId));
        job.setFuns(funs);
    }


    public List<Function> funs(Integer funTypeId){
        return fm.funs(funTypeId);
    }


}
